package com.catering.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.catering.dao.CateringCartDao;
import com.catering.dao.CateringSpacesChildDao;
import com.catering.entity.CateringCart;
import com.catering.entity.CateringSpacesChild;
import com.catering.service.CateringCartService;
import com.catering.until.CommonUtils;
import com.catering.until.ResultData;
import com.catering.until.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class CateringCartServiceImpl extends ServiceImpl<CateringCartDao, CateringCart> implements CateringCartService {
	
	@Autowired
	private CateringCartDao cateringCartDao;
	@Autowired
	private CateringSpacesChildDao cateringSpacesChildDao;

	@Override
	public ResultData select(Map<String,String> map) {
		try {
			QueryWrapper<CateringCart> wrapper = CommonUtils.getKeySet(map,"t1");
			List<CateringCart> list = new ArrayList<CateringCart>();
			Integer count = 0;
			if(CommonUtils.checkNoNull(map.get("page"))) {
				IPage<CateringCart> page = new Page<>(Integer.parseInt(map.get("page")),Integer.parseInt(map.get("limit")));
				IPage<CateringCart> pageList = cateringCartDao.selectPageList(page, wrapper);
				count = cateringCartDao.selectPageCount(wrapper);
				list = pageList.getRecords();
			}else {
				list = cateringCartDao.selectNoPageList(wrapper);
			}
			for(CateringCart cart : list){
				if(CommonUtils.checkNoNull(cart.getSpacesId())){
					CateringSpacesChild cw = new CateringSpacesChild();
					cw.setSysIds(Arrays.stream(cart.getSpacesId().split(",")).collect(Collectors.toList()));
					List<CateringSpacesChild> childList = cateringSpacesChildDao.selectNameList(cw);
					cart.setSpacesName(childList.stream().map(CateringSpacesChild::getSpacesName).collect(Collectors.joining(",")));
				}
			}
			return ResultData.success(list,count);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.SERVER_ERROR.getCode(),ResultEnum.SERVER_ERROR.getMsg());
	}

	@Override
	public ResultData insert(Map<String,String> map) {
		try {
			CateringCart vo = JSON.parseObject(JSON.toJSONString(map), CateringCart.class);
			vo.setCreateTime(CommonUtils.getTime(19));
			cateringCartDao.insert(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.INSERT_ERROR.getCode(),ResultEnum.INSERT_ERROR.getMsg());
		}
	}

	@Override
	public ResultData update(Map<String,String> map) {
		try {
			CateringCart vo = JSON.parseObject(JSON.toJSONString(map), CateringCart.class);
			cateringCartDao.updateById(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.UPDATE_ERROR.getCode(),ResultEnum.UPDATE_ERROR.getMsg());
		}
	}

	@Override
	public ResultData delete(Map<String,Object> map) {
		try {
			CateringCart vo = JSON.parseObject(JSON.toJSONString(map), CateringCart.class);
			if(CommonUtils.checkNoNull(vo.getUserId())){
				cateringCartDao.delete(new LambdaQueryWrapper<CateringCart>().eq(CateringCart::getUserId,vo.getUserId()));
			}else{
				List<String> list = Stream.of(vo.getSysId().split(",")).collect(Collectors.toList());
				cateringCartDao.deleteBatchIds(list);
			}
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.DELETE_ERROR.getCode(),ResultEnum.DELETE_ERROR.getMsg());
	}
}
